package com.shx.bill.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    @PostMapping("/login")
    public String login(HttpSession session, String username, String password, Map<String, Object> map) {
        if (!StringUtils.isEmpty(username) && "123".equals(password)) {
            session.setAttribute("username", username);
            return "redirect:/main.html";
        }

        // 登录失败
        map.put("msg", "用户名或密码不正确");
        return "main/login";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("username");
        session.invalidate();
        return "redirect:/login";
    }

}
